
//  HTMenuHeaderView.m
//  HT_aigongzuo
//
//  Created by 周昭 on 16/8/23.
//  Copyright © 2016年 HT_Technology. All rights reserved.
//

#import "HTMenuHeaderView.h"

@interface HTMenuHeaderView()
/**
 *  背景Img
 */
@property (nonatomic, weak) UIImageView *bgImgView;

/**
 *  头像
 */
@property (nonatomic, weak) UIImageView *iconImgView;

/**
 *  姓名
 */
@property (nonatomic, weak) UILabel *nameLabel;

/**
 *  个人简介
 */
@property (nonatomic, weak) UILabel *introLabel;

@end

@implementation HTMenuHeaderView

/**
 *  通过代码初始化
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpSubviews];
    }
    return self;
}

/**
 *  通过xib/解档初始化加载
 */
- (instancetype)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubviews];
    }
    return self;
}

/**
 *  初始化所有子控件&进行一次性设置
 */
- (void)setUpSubviews
{
    UIImageView *bgImgView = [[UIImageView alloc] init];
    bgImgView.image = [UIImage imageWithName:@"bgImgView"];
    [self addSubview:bgImgView];
    self.bgImgView = bgImgView;
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.layer.masksToBounds = YES;
    [self addSubview:iconImgView];
    self.iconImgView = iconImgView;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = [UIFont boldSystemFontOfSize:16.5f];
    [self addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UILabel *introLabel = [[UILabel alloc] init];
    introLabel.textColor = [UIColor blackColor];
    introLabel.textAlignment = NSTextAlignmentLeft;
    introLabel.font = [UIFont systemFontOfSize:15.5f];
    introLabel.numberOfLines = 0;
    [self addSubview:introLabel];
    self.introLabel = introLabel;
    
    [self setUpUserInfoData];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewTap:)];
    [self addGestureRecognizer:tap];
}

/**
 *  监听self的点击事件
 */
- (void)bgViewTap:(UITapGestureRecognizer *)tap
{
    if ([self.delegate respondsToSelector:@selector(menuHeaderViewDidClickBgView)]) {
        [self.delegate menuHeaderViewDidClickBgView];
    }
}

/**
 *  拿到真实尺寸进行设置
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat bgImgViewX = 0;
    CGFloat bgImgViewY = 0;
    CGFloat bgImgViewW = self.frame.size.width;
    CGFloat bgImgViewH = self.frame.size.height;
    self.bgImgView.frame = CGRectMake(bgImgViewX, bgImgViewY, bgImgViewW, bgImgViewH);
    
    CGFloat iconImgViewW = kScreenWidth == 568 ? 60 : 70;
    CGFloat iconImgViewX = 15 + iconImgViewW * 0.5;
    CGFloat iconImgViewY = 45 + iconImgViewW * 0.5;
    CGFloat iconImgViewH = iconImgViewW;
    self.iconImgView.layer.cornerRadius = iconImgViewW / 2;
    self.iconImgView.center = CGPointMake(iconImgViewX, iconImgViewY);
    self.iconImgView.bounds = CGRectMake(0, 0, iconImgViewW, iconImgViewH);
    
    CGFloat nameLabelW = self.frame.size.width - iconImgViewX - iconImgViewW * 0.5 - 30;
    CGFloat nameLabelX = iconImgViewX + iconImgViewW * 0.5 + nameLabelW * 0.5 + 10;
    CGFloat nameLabelY = iconImgViewY;
    CGFloat nameLabelH = 30;
    self.nameLabel.center = CGPointMake(nameLabelX, nameLabelY);
    self.nameLabel.bounds = CGRectMake(0, 0, nameLabelW, nameLabelH);
    
    CGFloat introLabelX = 20;
    CGFloat introLabelY = CGRectGetMaxY(self.iconImgView.frame);
    CGFloat introLabelW = self.frame.size.width - 2 * introLabelX;
    CGFloat introLabelH = 50;
    self.introLabel.frame = CGRectMake(introLabelX, introLabelY, introLabelW, introLabelH);
}

#pragma mark - 设置用户数据
- (void)setUpUserInfoData
{
    // 根据token的值来判断界面样式以及数据
    if (![KYUserAccountTool shareUserData].token) { // 如果token有值设置数据样式
        self.iconImgView.image = [UIImage imageWithColor:KYColor(100, 100, 100, 1.0)];
        self.nameLabel.text = NSLocalizedString(@"未登录", nil);
    } else {
        // 1.设置头像
        if ([KYUserAccountTool shareUserData].headImg) {
            [self.iconImgView setImageWithURL:[NSURL URLWithString:[KYUserAccountTool shareUserData].headImg]];
        } else {
            self.iconImgView.image = [UIImage imageWithName:@"me"];
        }
        
        // 2.设置昵称
        if ([KYUserAccountTool shareUserData].userName) {
            self.nameLabel.text = [KYUserAccountTool shareUserData].userName;
        }
        
        // 3.设置个性签名
        if ([KYUserAccountTool shareUserData].userIntro.length) {
            self.introLabel.text = [KYUserAccountTool shareUserData].userIntro;
        } else {
            self.introLabel.text = NSLocalizedString(@"该用户很懒,什么都没有留下...", nil);
        }
    }

}
@end
